package com.mdnote.jianzhi_offer.tree;

/**
 * @author Rhythm-2019
 * @version 1.0
 * @date 2020/11/10 18:38
 * @Email rhythm_2019@163.com
 * @Description 剑指 Offer 26. 树的子结构
 */
public class Offer26 {
    public boolean isSubStructure(TreeNode A, TreeNode B) {
        // order
        if (A == null || B == null) {
            return false;
        }
        boolean res = false;
        if (A.val == B.val) {
            res =  check(A.left, B.left) && check(A.right, B.right);
        }
        if (!res) {
            res = isSubStructure(A.left, B) || isSubStructure(A.right, B);
        }

        return res;
    }

    public boolean check(TreeNode A, TreeNode B){
        if (B == null) {
            return true;
        }
        if (A == null) {
            return false;
        }
        if (A.val != B.val) {
            return false;
        }
        return  check(A.left, B.left) && check(A.right, B.right);
    }

    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode(int x) { val = x; }
    }
}
